#include <cstdio>
#include <cassert>
using namespace std;
typedef long long ll;
const int N=10000003;
int fac[N],fiv[N];
int n,p;
int read(){
	char c=getchar();int x=0;
	while(c<48||c>57) c=getchar();
	do x=(x<<1)+(x<<3)+(c^48),c=getchar();
	while(c>=48&&c<=57);
	return x;
}
int qp(int a,int b=p-2){
	int res=1;
	while(b){
		if(b&1) res=(ll)res*a%p;
		a=(ll)a*a%p;b>>=1;
	}
	return res;
}
int main(){
	freopen("tree.in","r",stdin);
	freopen("tree.out","w",stdout);
	n=read();p=read();assert(n<p);
	fac[0]=1;
	for(int i=1;i<=n;++i) fac[i]=(ll)fac[i-1]*i%p;
	fiv[n]=qp(fac[n]);
	for(int i=n;i;--i) fiv[i-1]=(ll)fiv[i]*i%p;
	int res=fac[n];
	int bn=1,pw=1;
	for(int k=1;k<n;++k){
		bn+=bn;
		if(bn>=p) bn-=p;
		res=(res+(ll)fac[n]*fiv[k]%p*bn%p*(n-k)%p*pw)%p;
		pw=(ll)pw*n%p;
	}
	printf("%d\n",res);
	return 0;
}
